summaryrefslogtreecommitdiffstats
path: root/src/audio_core/device/audio_buffers.h
blob: 15082f6c62dd44d9b700c50866e128ef0e95d381 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <array>
#include <mutex>
#include <span>
#include <vector>

#include "audio_buffer.h"
#include "audio_core/device/device_session.h"
#include "core/core_timing.h"

namespace AudioCore {

constexpr s32 BufferAppendLimit = 4;

/**
 * A ringbuffer of N audio buffers.
 * The buffer contains 3 sections:
 *     Appended   - Buffers added to the ring, but have yet to be sent to the audio backend.
 *     Registered - Buffers sent to the backend and queued for playback.
 *     Released   - Buffers which have been played, and can now be recycled.
 * Any others are free/untracked.
 *
 * @tparam N - Maximum number of buffers in the ring.
 */
template <size_t N>
class AudioBuffers {
public:
    explicit AudioBuffers(size_t limit) : append_limit{static_cast<u32>(limit)} {}

    /**
     * Append a new audio buffer to the ring.
     *
     * @param buffer - The new buffer.
     */
    void AppendBuffer(const AudioBuffer& buffer) {
        std::scoped_lock l{lock};
        buffers[appended_index] = buffer;
        appended_count++;
        appended_index = (appended_index + 1) % append_limit;
    }

    /**
     * Register waiting buffers, up to a maximum of BufferAppendLimit.
     *
     * @param out_buffers - The buffers which were registered.
     */
    void RegisterBuffers(std::vector<AudioBuffer>& out_buffers) {
        std::scoped_lock l{lock};
        const s32 to_register{std::min(std::min(appended_count, BufferAppendLimit),
                                       BufferAppendLimit - registered_count)};

        for (s32 i = 0; i < to_register; i++) {
            s32 index{appended_index - appended_count};
            if (index < 0) {
                index += N;
            }

            out_buffers.push_back(buffers[index]);
            registered_count++;
            registered_index = (registered_index + 1) % append_limit;

            appended_count--;
            if (appended_count == 0) {
                break;
            }
        }
    }

    /**
     * Release a single buffer. Must be already registered.
     *
     * @param index     - The buffer index to release.
     * @param timestamp - The released timestamp for this buffer.
     */
    void ReleaseBuffer(s32 index, s64 timestamp) {
        std::scoped_lock l{lock};
        buffers[index].played_timestamp = timestamp;

        registered_count--;
        released_count++;
        released_index = (released_index + 1) % append_limit;
    }

    /**
     * Release all registered buffers.
     *
     * @param core_timing - The CoreTiming instance
     * @param session     - The device session
     *
     * @return If any buffer was released.
     */
    bool ReleaseBuffers(const Core::Timing::CoreTiming& core_timing, const DeviceSession& session,
                        bool force) {
        std::scoped_lock l{lock};
        bool buffer_released{false};
        while (registered_count > 0) {
            auto index{registered_index - registered_count};
            if (index < 0) {
                index += N;
            }

            // Check with the backend if this buffer can be released yet.
            // If we're shutting down, we don't care if it's been played or not.
            if (!force && !session.IsBufferConsumed(buffers[index])) {
                break;
            }

            ReleaseBuffer(index, core_timing.GetGlobalTimeNs().count());
            buffer_released = true;
        }

        return buffer_released || registered_count == 0;
    }

    /**
     * Get all released buffers.
     *
     * @param tags - Container to be filled with the released buffers' tags.
     * @return The number of buffers released.
     */
    u32 GetReleasedBuffers(std::span<u64> tags) {
        std::scoped_lock l{lock};
        u32 released{0};

        while (released_count > 0) {
            auto index{released_index - released_count};
            if (index < 0) {
                index += N;
            }

            auto& buffer{buffers[index]};
            released_count--;

            auto tag{buffer.tag};
            buffer.played_timestamp = 0;
            buffer.samples = 0;
            buffer.tag = 0;
            buffer.size = 0;

            if (tag == 0) {
                break;
            }

            tags[released++] = tag;

            if (released >= tags.size()) {
                break;
            }
        }

        return released;
    }

    /**
     * Get all appended and registered buffers.
     *
     * @param buffers_flushed - Output vector for the buffers which are released.
     * @param max_buffers     - Maximum number of buffers to released.
     * @return The number of buffers released.
     */
    u32 GetRegisteredAppendedBuffers(std::vector<AudioBuffer>& buffers_flushed, u32 max_buffers) {
        std::scoped_lock l{lock};
        if (registered_count + appended_count == 0) {
            return 0;
        }

        size_t buffers_to_flush{
            std::min(static_cast<u32>(registered_count + appended_count), max_buffers)};
        if (buffers_to_flush == 0) {
            return 0;
        }

        while (registered_count > 0) {
            auto index{registered_index - registered_count};
            if (index < 0) {
                index += N;
            }

            buffers_flushed.push_back(buffers[index]);

            registered_count--;
            released_count++;
            released_index = (released_index + 1) % append_limit;

            if (buffers_flushed.size() >= buffers_to_flush) {
                break;
            }
        }

        while (appended_count > 0) {
            auto index{appended_index - appended_count};
            if (index < 0) {
                index += N;
            }

            buffers_flushed.push_back(buffers[index]);

            appended_count--;
            released_count++;
            released_index = (released_index + 1) % append_limit;

            if (buffers_flushed.size() >= buffers_to_flush) {
                break;
            }
        }

        return static_cast<u32>(buffers_flushed.size());
    }

    /**
     * Check if the given tag is in the buffers.
     *
     * @param tag - Unique tag of the buffer to search for.
     * @return True if the buffer is still in the ring, otherwise false.
     */
    bool ContainsBuffer(const u64 tag) const {
        std::scoped_lock l{lock};
        const auto registered_buffers{appended_count + registered_count + released_count};

        if (registered_buffers == 0) {
            return false;
        }

        auto index{released_index - released_count};
        if (index < 0) {
            index += append_limit;
        }

        for (s32 i = 0; i < registered_buffers; i++) {
            if (buffers[index].tag == tag) {
                return true;
            }
            index = (index + 1) % append_limit;
        }

        return false;
    }

    /**
     * Get the number of active buffers in the ring.
     * That is, appended, registered and released buffers.
     *
     * @return Number of active buffers.
     */
    u32 GetAppendedRegisteredCount() const {
        std::scoped_lock l{lock};
        return appended_count + registered_count;
    }

    /**
     * Get the total number of active buffers in the ring.
     * That is, appended, registered and released buffers.
     *
     * @return Number of active buffers.
     */
    u32 GetTotalBufferCount() const {
        std::scoped_lock l{lock};
        return static_cast<u32>(appended_count + registered_count + released_count);
    }

    /**
     * Flush all of the currently appended and registered buffers
     *
     * @param buffers_released - Output count for the number of buffers released.
     * @return True if buffers were successfully flushed, otherwise false.
     */
    bool FlushBuffers(u32& buffers_released) {
        std::scoped_lock l{lock};
        std::vector<AudioBuffer> buffers_flushed{};

        buffers_released = GetRegisteredAppendedBuffers(buffers_flushed, append_limit);

        if (registered_count > 0) {
            return false;
        }

        if (static_cast<u32>(released_count + appended_count) > append_limit) {
            return false;
        }

        return true;
    }

    u64 GetNextTimestamp() const {
        // Iterate backwards through the buffer queue, and take the most recent buffer's end
        std::scoped_lock l{lock};
        auto index{appended_index - 1};
        if (index < 0) {
            index += append_limit;
        }
        return buffers[index].end_timestamp;
    }

private:
    /// Buffer lock
    mutable std::recursive_mutex lock{};
    /// The audio buffers
    std::array<AudioBuffer, N> buffers{};
    /// Current released index
    s32 released_index{};
    /// Number of released buffers
    s32 released_count{};
    /// Current registered index
    s32 registered_index{};
    /// Number of registered buffers
    s32 registered_count{};
    /// Current appended index
    s32 appended_index{};
    /// Number of appended buffers
    s32 appended_count{};
    /// Maximum number of buffers (default 32)
    u32 append_limit{};
};

} // namespace AudioCore